package sanrenxing.dayEnd.backRepertoryTotal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sanrenxing.dayEnd.backRepertoryTotal.domain.Foundation;
import sanrenxing.dayEnd.backRepertoryTotal.service.impl.FoundationBizImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：基金控制器类
 * 编写者：蒋玲苹
 * 日期：21-03-18
 * 版本：1.0
 */
@RestController
@RequestMapping("/foundcrud")
public class FoundationController {
    /*定义业务层接口*/
    @Autowired
    private FoundationBizImpl foundationBiz;

    /**
     * 显示数据
     * @param page 页数
     * @param limit 行数
     * @param foundation 基金对象
     * @return
     */
    @GetMapping("/fund")
    public Map<String,Object>  queryAll(int page, int limit, Foundation foundation){
        //创建Map集合对象
        Map<String,Object> map=new HashMap<>();
        //创建LIST集合对象，分页查询
        List<Foundation> foundations=foundationBiz.queryAll(foundation,limit,(page-1)*limit);
        //在映射集合中添加key为code，值为0的状态码
        map.put("code",0);
        //在映射集合中添加key为msg,值为响应信息“ ”字符串
        map.put("msg","");
        //在映射集合中添加key为count
        map.put("count",foundationBiz.count(foundation));
        //在映射集合中添加key为data
        map.put("data",foundations);
        //在映射集合中添加key为ca
        map.put("ca",foundationBiz.ca());
        //在映射集合中添加key为ms
        map.put("ms",foundationBiz.ms());
        //在映射集合中添加key为ts
        map.put("ts",foundationBiz.ts());
        //返回
        return map;
    }

    /**
     * 添加数据
     * @param foundation 基金对象
     * @return  添加成功："add success"  添加失败："failure"
     */
    @PostMapping("/fund")
    public String addFund(@RequestBody Foundation foundation) {
        //调用foundationBiz的添加方法
        int rows=foundationBiz.add(foundation);
        //返回
        return rows>0?"add success":"failure";
    }

    /**
     * 删除数据
     * @param str
     * @return  删除成功："delete success" 删除失败："delete failure"
     */
    @DeleteMapping("/fund/{str}")
    public String deleteFund(@PathVariable String str){
        //调用业务层删除数据
        int rows=foundationBiz.delete(str);
        //返回
        return rows>0?"delete success":"delete failure";
    }

    /**
     * 修改数据
     * @param foundation 基金对象
     * @return  修改成功："update success" 修改失败："update failure"
     */
    @PutMapping("/fund")
    public String updateFund(@RequestBody Foundation foundation){
        //调用业务层修改数据
        int rows=foundationBiz.update(foundation);
        //返回
        return rows>0?"update success":"update failure";
    }
}

